package com.yexiao.cofe.common.exception;

import cn.hutool.core.util.StrUtil;
import com.yexiao.core.response.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 业务异常
     */
    @ExceptionHandler(BusinessException.class)
    public ResponseData exceptionHandler(BusinessException e) {
        logger.error(StrUtil.format("全局页面异常捕获，{},",  e));
        e.printStackTrace();
        return ResponseData.fail(e.getCode(), e.getMsg());
    }

    @ExceptionHandler(Exception.class)
    public ResponseData exceptionHandler(Exception ex) {
        logger.error(StrUtil.format("全局页面异常捕获，{},",  ex));
        ex.printStackTrace();
        return ResponseData.fail(ex.getMessage());
    }
}
